package com.digitalchina.lxnsy.activity.old;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.TextView;

import com.digitalchina.lxnsy.R;
import com.digitalchina.lxnsy.api.BusinessCollect;
import com.digitalchina.lxnsy.api.BusinessInformation;
import com.digitalchina.lxnsy.base.BaseActivity;
import com.digitalchina.lxnsy.constant.MSG;
import com.digitalchina.lxnsy.entity.FarmingInfoListVO;
import com.digitalchina.lxnsy.toolkit.FastJsonUtil;
import com.digitalchina.lxnsy.toolkit.FrescoUtil;
import com.facebook.drawee.view.SimpleDraweeView;
import com.zzhoujay.richtext.RichText;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * ================================================
 * 项目名称：BigData
 * 类描述：
 * 包名：com.digitalchina.bigdata.activity
 * 创建人：郑皓
 * 创建时间：2018/8/7 10:14
 * 修改备注：
 * ================================================
 */
public class FarmingInfoDetailActivity extends BaseActivity {

    @Bind(R.id.title_tv)
    TextView tvTitle;
    @Bind(R.id.type_tv)
    TextView tvType;
    @Bind(R.id.time_tv)
    TextView tvTime;
    @Bind(R.id.browse_tv)
    TextView tvBrowse;
    @Bind(R.id.tv_content)
    TextView tvContent;
    @Bind(R.id.iv_logo)
    SimpleDraweeView ivLogo;

    private FarmingInfoListVO farmingInfoListVO;

    @Override
    public void setLayout() {
        setContentLayout(R.layout.activity_farm_info_detail);
        setTitle("资讯详情");

        Drawable img = getResources().getDrawable(R.drawable.ic_v_collect);
        img.setBounds(0, 0, img.getMinimumWidth()/2, img.getMinimumHeight()/2);
        mTvRight.setCompoundDrawables(img, null, null, null);
        mTvRight.setText("收藏");
        mTvRight.setVisibility(View.VISIBLE);
        RichText.initCacheDir(this);
    }

    @Override
    public void initMember() {

        String id ="";
        if (getIntent().hasExtra("FarmingInfoListVO")) {
            farmingInfoListVO = (FarmingInfoListVO) getIntent().getSerializableExtra("FarmingInfoListVO");
            id = farmingInfoListVO.getId();
        } else {
            id = (String) getIntent().getSerializableExtra("contentId");
        }
        BusinessInformation.getInfoDetailById(this, id, mHandler);
    }

    private void setView() {
        FrescoUtil.showImageSmall(farmingInfoListVO.getImgUrl(), ivLogo);
        tvType.setText(farmingInfoListVO.getNewsTypeName() + "");
        tvTitle.setText(farmingInfoListVO.getTitle() + "");
        tvTime.setText(farmingInfoListVO.getCreateTime() + "");
        tvBrowse.setText(farmingInfoListVO.getPageViewNums()==null? "浏览量：0":"浏览量："+farmingInfoListVO.getPageViewNums());


        RichText.fromHtml(farmingInfoListVO.getContent()).into(tvContent);

       // tvContent.setText(farmingInfoListVO.getContent() != null ? StringUtil.stripHtml(farmingInfoListVO.getContent()) : "");
    }

    @Override
    public void addListener() {

    }

    @Override
    public void setHandler() {
        mHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                switch (msg.what) {
                    case MSG.MSG_ADD_STORE_SUCCESS:
                        showToast("收藏成功");
                        break;
                    case MSG.MSG_GET_INFOMATION_DETAIL_SUCCESS:
                        farmingInfoListVO = FastJsonUtil.getBeanData(msg.obj.toString(), FarmingInfoListVO.class);
                        setView();
                        break;
                    case MSG.MSG_ADD_STORE_FIELD:
                    case MSG.MSG_GET_INFOMATION_DETAIL_FIELD:
                        showToast(msg.obj.toString());
                        break;
                }
            }
        };
    }

    @Override
    public void callBack(Object object) {

    }

    @Override
    public void rightOnClick() {
        if (farmingInfoListVO == null)
            return;

        if (noLoginToSkip()){
            BusinessCollect.addStore(this, farmingInfoListVO.id, "3", mHandler);
        }
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
